<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends BaseController
{
    public function login(Request $request)
    {
        $username = $request->post('username');
        $password = $request->post('password');

        $user = User::query()->whereUsername($username)->first();

        if (empty($user)) return self::responseMsg("用户不存在");
        if ($user->password != md5($password)) return self::responseMsg("用户密码不正确 => " . md5($password));

        $user->token = md5(time() + $password);
        $user->save();

        return self::responseJson(['token' => $user->token, 'id' => $user->id]);
    }

    public function info(Request $request)
    {
//        $user = Auth::user();
        return self::responseJson([
//            'user'   => $user,
            'name'   => 'root',
            'avatar' => 'https://p.qqan.com/up/2018-9/15367146891677274.jpg',
            'roles'  => ['administrator'],
        ]);
    }

    public function logout(Request $request)
    {
        $user = Auth::user();

        return self::responseJson($user);
    }
}
